<template>
	<dt-main-body v-model="labelPosition">
		<div class="location">
			<el-breadcrumb separator-class="el-icon-arrow-right">
				<el-breadcrumb-item>
					<i class="iconfont icon-home"></i>
					管理中心
				</el-breadcrumb-item>
				<el-breadcrumb-item>商户管理</el-breadcrumb-item>
				<el-breadcrumb-item>编辑类型</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="content-box">
			<el-form ref="editForm" :model="model" :rules="rules" :label-position="labelPosition" label-width="120px">
				<el-tabs v-model="activeName" type="card">
					<el-tab-pane label="基本信息" name="info">
						<div class="tab-content">
							<el-form-item prop="title" label="类型名称">
								<el-input v-model="model.title" placeholder="必填，128字符内"></el-input>
							</el-form-item>
							<el-form-item prop="remark" label="要求说明">
								<el-input type="textarea"
									show-word-limit :maxlength="512" :rows="3" v-model="model.remark" placeholder="非必填，店铺类型的要求说明">
								</el-input>
							</el-form-item>
							<el-form-item prop="sortId" label="排序数字">
								<el-input v-model="model.sortId" placeholder="数字越小越排前"></el-input>
							</el-form-item>
							<el-form-item label="创建时间" v-if="model.id>0">
								{{model.addBy}} {{model.addTime}}
							</el-form-item>
						</div>
					</el-tab-pane>
				</el-tabs>
			</el-form>
		</div>
		<div class="footer-box">
			<div class="footer-btn">
				<el-button type="primary" :loading="loading" @click="submitForm">确认保存</el-button>
				<el-button plain @click="$common.back()">返回上一页</el-button>
			</div>
		</div>
	</dt-main-body>
</template>

<script>
	export default {
		props: {
			id: 0
		},
		data() {
			return {
				activeName: 'info',//当前选项卡
				labelPosition: 'right',//表单对齐方式
				loading: false,
				model: {
					id: 0,
					title: null,
					remark: null,
					sortId: 99,
					addTime: null,
				},
				rules: {
					title: [
						{ required: true, message: '请输入品牌名称', trigger: 'blur' }
					]
				}
			}
		},
		methods: {
			//初始化数据
			initData() {
				let _this = this;
				if (_this.id) {
					_this.$api.request({
						url: `/admin/merchant/store/${_this.id}`,
						loading: true,
						success(res) {
							//赋值给model
							Object.keys(_this.model).forEach(key => { _this.model[key] = res.data[key] });
						}
				    });
				}
			},
			//提交表单
			submitForm() {
				let _this = this;
				//多个表单验证
				const f1 = new Promise((resolve, reject) => {
					_this.$refs['editForm'].validate((valid) => {
						if (valid) resolve();
					});
				});
				Promise.all([f1]).then(() => {
					//提交表单,Id大于0则修改，否则添加
				    if (_this.model.id > 0) {
						_this.$api.request({
							method: 'put',
							url: `/admin/merchant/store/${_this.model.id}`,
							data: _this.model,
							successMsg: '修改类别已成功',
							beforeSend() {
								_this.loading = true;
							},
							success(res) {
								_this.$common.linkUrl('/merchant/store/list');
							},
							complete() {
								_this.loading = false;
							}
						});
					} else {
						_this.$api.request({
							method: 'post',
							url: '/admin/merchant/store',
							data: _this.model,
							successMsg: '新增类别已成功',
							beforeSend() {
								_this.loading = true;
							},
							success(res) {
								_this.$refs['editForm'].resetFields(); //重置表单
							},
							complete() {
								_this.loading = false;
							}
						});
					}
				})
			},
		},
		created() {
			//初始化数据
			this.initData();
		}
	}
</script>